/*
 * Copyright 2020-2035 the original author or authors.
 *
 */

package com.econvent.common.utils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * 通用的时间管理类
 *
 * @author xiekun
 * @since 1.0
 */
public class TimeUtil {
    /**
     * 默认的日期格式化format
     * **/
    static final String DEFAULT_DATE_FORMTAE_STR = "yyyy-MM-dd" ;

    /**
     * 默认的时间格式化format
     * **/
    static final String DEFAULT_TIMEMIN_FORMTAE_STR = "HH:mm" ;

    /**
     * 默认的时间格式化format
     * **/
    static final String DEFAULT_TIME_FORMTAE_STR = "HH:mm:ss" ;

    /**
     * 默认的日期时间格式化format
     * **/
    static final String DEFAULT_DATETIME_FORMTAE_STR = "yyyy-MM-dd HH:mm:ss" ;

    /**
     * 默认当月的开始日期
     * **/
    private static Integer DEFAULT_MONTH_DAY = 1 ;

    /**
     * 获取当前日期字符串
     * **/
    public static String getCurrentDate() {
        LocalDate nowDate = LocalDate.now();
        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMTAE_STR);
        return dateFormate.format(nowDate) ;
    }

    /**
     * 获取当前日期时间字符串
     * **/
    public static String getCurrentDateTime() {
        LocalDateTime nowDateTime = LocalDateTime.now();
        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_DATETIME_FORMTAE_STR);
        return dateFormate.format(nowDateTime) ;
    }

    /**
     * 获取当前日期仅包含时间不包含S部分
     * **/
    public static String getCurrentOnlyTimeMin() {
        LocalDateTime nowDateTime = LocalDateTime.now();
        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_TIMEMIN_FORMTAE_STR);
        return dateFormate.format(nowDateTime) ;
    }

    /**
     * 获取当前日期仅包含时间部分
     * **/
    public static String getCurrentOnlyTime() {
        LocalDateTime nowDateTime = LocalDateTime.now();
        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMTAE_STR);
        return dateFormate.format(nowDateTime) ;
    }

    /**
     * 获取当前周的周一日期
     * **/
    public static String getCurWeekBeginDate() {
        LocalDate curWeekBeginDate = LocalDate.now().with(DayOfWeek.MONDAY) ;
        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMTAE_STR);
        return dateFormate.format(curWeekBeginDate) ;
    }

    /**
     * 获取当前周的周日日期
     * **/
    public static String getCurWeekEndDate() {
        LocalDate curWeekEndDate = LocalDate.now().with(DayOfWeek.SUNDAY) ;
        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMTAE_STR);
        return dateFormate.format(curWeekEndDate) ;
    }

    /**
     * 获取当前月份的开始日期
     * **/
    public static String getCurMonthBeginDate() {
        LocalDate curMonthBeginDate = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()) ;
        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMTAE_STR);
        return dateFormate.format(curMonthBeginDate) ;
    }

    /**
     * 获取当前月份的结束日期
     * **/
    public static String getCurMonthEndDate() {
        LocalDate curMonthBeginDate = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()) ;
        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMTAE_STR);
        return dateFormate.format(curMonthBeginDate) ;
    }

    /**
     * 获取当前季度的开始日期
     * **/
    public static String getCurSeasonBeginDate() {
        Month tarMonth = LocalDate.now().getMonth().firstMonthOfQuarter() ;
        LocalDate curSeasonBeginMonth = LocalDate.now().withMonth(tarMonth.getValue()).
                with(TemporalAdjusters.firstDayOfMonth()) ;

        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMTAE_STR);
        return dateFormate.format(curSeasonBeginMonth) ;
    }

    /**
     * 获取当前季度的结束日期
     * **/
    public static String getCurSeasonEndDate() {
        Month tarMonth = LocalDate.now().getMonth().firstMonthOfQuarter().plus(3);
        LocalDate curSeasonEndDate = LocalDate.now().withMonth(tarMonth.getValue()).
                with(TemporalAdjusters.lastDayOfMonth()) ;

        DateTimeFormatter dateFormate  = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMTAE_STR);

        return dateFormate.format(curSeasonEndDate) ;
    }
}
